package com.teamwings.ctrls.formCtrls;

import com.teamwings.common.aspect.annotation.FormControl;
import com.teamwings.common.constant.FormConstant;
import com.teamwings.common.vo.Result;
import com.teamwings.ctrls.FormComponent;
import com.teamwings.modules.design.vo.FormVo;
import com.teamwings.modules.page.service.IPageService;
import lombok.Data;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;
import java.util.Calendar;

/**
 * @author wuyc
 * 办文编号
 */
@Data
@FormControl(type = "oa_doc_code")
public class DocCode extends FormComponent {
    public DocCode() {
        this.type = "oa_doc_code";
        this.fieldDefaultLength = "20";
        this.fieldType = "VARCHAR";
    }

    @Override
    public synchronized Result<?> trigger(FormConstant.TRIGGER_TYPE type, IPageService pageService,
                             FormVo formDesignCode, Object paramsData) {
        String docTitle = paramsData.toString();

        // 获取最新的办文编号
        JdbcTemplate jdbcTemplate = pageService.getJdbcTemplate();
        Map<String, Object> map = jdbcTemplate
                .queryForMap("select item_value, description from sys_dict_item where item_text = ?",
                        new Object[]{docTitle});

        String docCode = "";
        String codeDesc = "";
        if (map != null && map.size() > 0) {
            docCode = map.get("item_value").toString();
            codeDesc = map.get("description").toString();
        }

        String nextDocCode;
        try {
            nextDocCode = this.generateCode(docCode, codeDesc);
        }catch (Exception e) {
            return Result.error("生成失败。");
        }

        // 更新到字典
        String sql = "update sys_dict_item set item_value = ? where item_text = ?";
        int updated = jdbcTemplate.update(sql, new Object[]{nextDocCode, docTitle});

        if (updated > 0) {
            return Result.OK("生成成功", nextDocCode);
        } else {
            return Result.error("生成失败，保存记录失败。");
        }
    }

    /**
     * 生成新的办文编号
     * @param currentCode
     * @param description
     * @return
     */
    private String generateCode(String currentCode, String description) {
        String yearDesc = "yyyy";
        int indexYear = description.indexOf("yyyy");
        int indexNumber = description.indexOf("#");
        int indexLastNumber = description.lastIndexOf("#");

        String currentYear = currentCode.substring(indexYear, 4 + indexYear);
        Calendar date = Calendar.getInstance();
        String systemYear = String.valueOf(date.get(Calendar.YEAR));

        // 如果年份一样，则继续序数
        String strNum = "0001";
        if (currentYear.equals(systemYear)) {
            Integer num = Integer.valueOf(currentCode.substring(indexNumber, indexLastNumber + 1));
            num++;
            strNum = String.format("%04d", num);
        }
        String nextCode = description.replaceAll(yearDesc, systemYear).replaceAll("[#]+", strNum);
        return nextCode;
    }
}
